﻿
namespace Orid.Management.SOHO.Book.Control.MPK_Calendar
{
    using System;
    using System.Collections.Generic;

   [FlagsAttribute]
   public  enum EBookPciker    { Empty = 0, 深夜 = 1, 早朝 = 2,午前 =4, 午後 = 8,夜=16  }


   /// <summary>
   /// Book Picker class
   /// </summary>
    public class BookPicker
    {
        private static readonly BookTime[] _bookTime = new []{
                 new BookTime {  From = new DateTime(1900, 1, 1, 0, 0, 0), To = new DateTime(1900, 1, 1, 3, 59, 59) },
                 new BookTime {  From = new DateTime(1900, 1, 1, 4, 0, 0), To = new DateTime(1900, 1, 1, 6, 59, 59) },
                 new BookTime { From = new DateTime(1900, 1, 1, 7, 0, 0), To = new DateTime(1900, 1, 1, 11, 59, 59) },
                 new BookTime { From = new DateTime(1900, 1, 1, 12, 0, 0), To = new DateTime(1900, 1, 1, 17, 59, 59) },
                 new BookTime { From = new DateTime(1900, 1, 1, 18, 0, 0), To = new DateTime(1900, 1, 1, 23, 59, 59) }
        };

        /// <summary>
        /// Gets the book time.
        /// </summary>
        /// <param name="picker">The picker.</param>
        /// <returns></returns>
        static public BookTime GetBookTime( EBookPciker picker)
        {
            return GetBookTime( Convert.ToInt32( picker) );
        }


        /// <summary>
        /// Gets the book time.
        /// </summary>
        /// <param name="picker">The picker.</param>
        /// <returns></returns>
        static public BookTime GetBookTime(int picker)
        {
            BookTime bookTime = null;
            switch (picker)
            {
                case 1:
                    bookTime = _bookTime[0];
                    break;
                case 2:
                    bookTime = _bookTime[1];
                    break;
                case 4:
                    bookTime = _bookTime[2];
                    break;
                case 8:
                    bookTime = _bookTime[3];
                    break;
                case 16:
                    bookTime = _bookTime[4];
                    break;

            }
            return bookTime;
        }


        /// <summary>
        /// Sets the book time.
        /// </summary>
        /// <param name="picker">The picker.</param>
        /// <param name="bookTime">The book time.</param>
        static public void SetBookTime(int picker,BookTime bookTime)
        {
            _bookTime[picker].From = bookTime.From;
            _bookTime[picker].To = bookTime.To;
        }


        /// <summary>
        /// 根据预约时间（多项）
        /// 得到枚举值（多项）
        /// </summary>
        /// <param name="bookTimes"></param>
        /// <returns></returns>
        static public  EBookPciker GetBookPicker(BookTime[]  bookTimes)
        {
            EBookPciker eBookPciker = EBookPciker.Empty;

            foreach (BookTime bookTime in bookTimes)
            {

                if (bookTime.From.Hour == _bookTime[0].From.Hour && bookTime.To.Hour == _bookTime[0].To.Hour)
                {
                    eBookPciker = eBookPciker | (EBookPciker)1;
                }
                else if (bookTime.From.Hour == _bookTime[1].From.Hour && bookTime.To.Hour == _bookTime[1].To.Hour)
                {
                    eBookPciker = eBookPciker | (EBookPciker)2;
                }
                else if (bookTime.From.Hour == _bookTime[2].From.Hour && bookTime.To.Hour == _bookTime[2].To.Hour)
                {
                    eBookPciker = eBookPciker | (EBookPciker)4;
                }
                else if (bookTime.From.Hour == _bookTime[3].From.Hour && bookTime.To.Hour == _bookTime[3].To.Hour)
                {
                    eBookPciker = eBookPciker | (EBookPciker)8;
                }
                else if (bookTime.From.Hour == _bookTime[4].From.Hour && bookTime.To.Hour == _bookTime[4].To.Hour)
                {
                    eBookPciker = eBookPciker | (EBookPciker)16;
                }

            }

            return eBookPciker;
        }



        /// <summary>
        /// Gets the book times.
        /// </summary>
        /// <param name="pickers">The pickers.</param>
        /// <returns></returns>
        static public List< BookTime > GetBookTimes( EBookPciker pickers)
        {
            List<BookTime> bookTimes = new List<BookTime>();

            if (( pickers & EBookPciker.深夜) == (EBookPciker)1 )
            {
                bookTimes.Add(GetBookTime(0));
            }

            if ((pickers & EBookPciker.早朝) == (EBookPciker)2)
            {
                bookTimes.Add(GetBookTime(2));
            }

            if ((pickers & EBookPciker.午前) == (EBookPciker)4)
            {
                bookTimes.Add(GetBookTime(4));
            }

            if ((pickers & EBookPciker.午後) == (EBookPciker)8)
            {
                bookTimes.Add(GetBookTime(8));
            }

            if ((pickers & EBookPciker.深夜) == (EBookPciker)16)
            {
                bookTimes.Add(GetBookTime(16));
            }

            return bookTimes;
        }
    }

    /// <summary>
    /// Book Time class
    /// </summary>
    public class BookTime
    {
        /// <summary>
        /// Gets or sets from.
        /// </summary>
        /// <value>From.</value>
        public  DateTime From { get; set; }

        /// <summary>
        /// Gets or sets to.
        /// </summary>
        /// <value>To.</value>
        public DateTime To { get; set; }
    }


    
}
